package main

import (
	"fmt"
	"log"
	"net"
)

func main() {

	// fmt.Println("开始")

	// queue := cellnet.NewEventQueue()

	// evd := socket.NewAcceptor(queue).Start(":8000")

	// socket.RegisterMessage(evd, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {

	// 	msg := content.(*gamedef.TestEchoACK)

	// 	fmt.Println("接收到信息", msg.String())

	// 	ses.Send(&gamedef.TestEchoACK{
	// 		Content: msg.String(),
	// 	})
	// })

	// queue.StartLoop()

	ln, err := net.Listen("tcp", ":6001")

	if err != nil {
		log.Println("服务错误" + err.Error())
	}

	go func() {

		for true {
			fmt.Println("goroutines")
			conn, err := ln.Accept()

			if err != nil {
				log.Println("服务错误" + err.Error())
			}
			log.Println("链接服务器", conn.RemoteAddr())

			go recvConnMsg(conn)
		}

	}()

	select {}

}

func recvConnMsg(conn net.Conn) {

	buf := make([]byte, 50)

	defer conn.Close()

	for {
		n, err := conn.Read(buf)

		if err != nil {
			log.Println("conn close")
			return
		}

		reviceData := string(buf[0:n])

		fmt.Println("recive data:" + reviceData)

		//data := "newip118.190.44.187:6001"

		//reader := bufio.NewReader(os.Stdin)

		//data, _, _ := reader.ReadLine()

		//command := []byte(data)

		//conn.Write(command)

		//log.Println("send msg" + string(data))

	}

}
